use container_core::{
    scheduler::simple_scheduler,
    workflow::{SimpleWorkflowEngine, WorkflowDocumentJsonReader},
};
use model_graph_types::{
    container::{
        sheduler::Scheduler,
        workflow::{
            WorkflowDocumentReadeOptions, WorkflowDocumentReader, WorkflowEngine,
            WorkflowEngineCompileOptions, WorkflowEngineRunOptions,
        },
    },
    database::{ConnectorProtocol, DataSource as DatabaseDataSource, DatabaseType},
    modeling::{
        scheduler::{
            SchedulerTaskBuilder, SchedulerTaskContentType, SchedulerTaskTemplateBuilder,
            SchedulerTaskType,
        },
        set_system_meta_database,
        workflow::{WorkflowContentType, WorkflowEntityBuilder, WorkflowType},
        Value,
    },
};
use std::borrow::BorrowMut;
use std::{collections::HashMap, thread::sleep, time::Duration};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    set_system_meta_database(DatabaseDataSource {
        id: 0,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/rrg01.sqlite"),
        ..Default::default()
    })?;

    let workflow = WorkflowEntityBuilder::default()
        .identifier("")
        .workflow_type(WorkflowType::DataFlow)
        .content_type(WorkflowContentType::File)
        .content("/Users/jinisu/rrg01/task_001.json")
        .build()?;

    let task_template = SchedulerTaskTemplateBuilder::default()
        .identifier("test_task_template_001")
        .name("测试")
        .title("测试")
        .task_type(SchedulerTaskType::Workflow)
        .content_type(SchedulerTaskContentType::Workflow)
        .content("workflow_test_task_001")
        .workflow(workflow)
        .build()?;

    let task = SchedulerTaskBuilder::default()
        .identifier("测试")
        .title("测试")
        .template_identifier("test_task_template_001")
        .cron("0/10 * * * * ?")
        .start_time(0_u32)
        .end_time(0_u32)
        .enable(true)
        .template(task_template)
        .build()?;

    let scheduler = simple_scheduler();
    // scheduler
    //     .lock()
    //     .map_err(|err| anyhow::anyhow!("{}", err))?
    //     .borrow_mut()
    //     .push_task(task)
    //     .await?;
    scheduler
        .lock()
        .map_err(|err| anyhow::anyhow!("{}", err))?
        .borrow_mut()
        .reload_tasks()
        .await?;

    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
